<template>
  <div>
    <el-table :data="list.filter(data => !search || data.deptName.toLowerCase().includes(search.toLowerCase()))" style="width: 100%">

      <el-table-column label="创建日期" prop="createTime"></el-table-column>

      <el-table-column label="部门名称" prop="deptName"></el-table-column>

      <el-table-column label="部门负责人" prop="bossName"></el-table-column>

      <el-table-column label="岗位数" prop="amount"></el-table-column>

      <el-table-column align="right">

        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索部门名称"/>
        </template>

        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="editBtn(scope.row.id)"></el-button>
          <el-button size="mini" @click="seeBtn(scope.row.id)">查看</el-button>
          <el-button size="mini" type="danger" @click="deleteBtn(scope.row.id)">删除</el-button>
        </template>

      </el-table-column>

    </el-table>

    <el-dialog title="岗位信息" :visible.sync="flag" width="500px">
      <el-table :data="position">
        <el-table-column property="createTime" label="创建时间" width="200"></el-table-column>
        <el-table-column property="position" label="岗位" width="200"></el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog title="编辑部门信息" :visible.sync="dialogFormVisible" width="350px">
      <el-form :model="form">
        <el-form-item label="部门名" label-width="100px">
          <el-input v-model="form.deptName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="部门领导" label-width="100px">
          <el-select v-model="form.bName" :placeholder="form.bossName">
            <el-option :label="obj.name" :value="obj.name" v-for="obj in leaderList" :key="obj.id"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="subBtn">确 定</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import {getDeptAndPosition,deleteDept,updateDept} from '@/api/core/dept/dept'
import {notLeader} from '@/api/core/manage/emp'

export default {
  data() {
    return {
      leaderList:[],
      form:{},
      list:[],
      search: '',
      position: [],
      flag:false,
      dialogFormVisible:false
    }
  },
  methods: {
    seeBtn(id) {
      const arr = this.list.filter(obj => obj.id === id)
      this.position = arr[0].children
      this.flag = true
    },

    deleteBtn(id) {
      const arr = this.list.filter(obj => obj.id === id)
      if (arr[0].amount > 0){
        this.$message({
          message: '部门有其岗位,请先删除岗位!',
          type: 'warning'
        });
      }else {
        this.$confirm('此操作将永久删除部门信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteDept(id).then((response) => {
            if(response.data.code === 200){
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getDeptList()
            }else {
              this.$message.error(response.data.message);
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    },

    async getDeptList(){
      const {data:res} = await getDeptAndPosition()
      this.list = res.data.list
      this.list.forEach( obj => obj.amount = obj.children.length)
    },

    editBtn(id){
      const arr = this.list.filter(obj => obj.id === id)
      this.form = arr[0]
      this.leaders()
      this.dialogFormVisible = true
    },

    async leaders(){
      const {data:res} = await notLeader()
      this.leaderList = res.data.list
    },

    async subBtn(){
      const {data:res} = await updateDept(this.form)
      if (res.code === 200){
        this.$message({
          message: '修改成功',
          type: 'success'
        });
      }
      await this.getDeptList()
      this.dialogFormVisible = false
    }
  },

  created() {
    this.getDeptList()
  }
}
</script>

<style scoped>

</style>
